package cn.iocoder.yudao.module.dbit.service.schmaj;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.dbit.controller.admin.schmaj.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.schmaj.SchMajDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dbit.dal.mysql.schmaj.SchMajMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dbit.enums.ErrorCodeConstants.*;

/**
 * 共建院校对应专业 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SchMajServiceImpl implements SchMajService {

    @Resource
    private SchMajMapper schMajMapper;

    @Override
    public Integer createSchMaj(SchMajSaveReqVO createReqVO) {
        // 插入
        SchMajDO schMaj = BeanUtils.toBean(createReqVO, SchMajDO.class);
        schMajMapper.insert(schMaj);
        // 返回
        return schMaj.getId();
    }

    @Override
    public void updateSchMaj(SchMajSaveReqVO updateReqVO) {
        // 校验存在
        validateSchMajExists(updateReqVO.getId());
        // 更新
        SchMajDO updateObj = BeanUtils.toBean(updateReqVO, SchMajDO.class);
        schMajMapper.updateById(updateObj);
    }

    @Override
    public void deleteSchMaj(Integer id) {
        // 校验存在
        validateSchMajExists(id);
        // 删除
        schMajMapper.deleteById(id);
    }

    private void validateSchMajExists(Integer id) {
        if (schMajMapper.selectById(id) == null) {
            throw exception(SCH_MAJ_NOT_EXISTS);
        }
    }

    @Override
    public SchMajDO getSchMaj(Integer id) {
        return schMajMapper.selectById(id);
    }

    @Override
    public PageResult<SchMajDO> getSchMajPage(SchMajPageReqVO pageReqVO) {
        return schMajMapper.selectPage(pageReqVO);
    }

}